package com.qdlc.p2p.biz.adapted.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.qd.p2p.adapter.AdaptePlatform;
import com.qd.p2p.sina.pay.enums.AccountType;
import com.qd.p2p.sina.pay.enums.CardAttribute;
import com.qd.p2p.sina.pay.enums.CardType;
import com.qd.p2p.sina.pay.enums.IdentityType;
import com.qd.p2p.sina.pay.enums.PayType;
import com.qd.p2p.sina.pay.param.CreateHostingDepositParam;
import com.qd.p2p.sina.pay.param.DepositExtendParam;
import com.qdlc.p2p.biz.processor.event.RechargeValueEvent;
import com.qdlc.p2p.biz.util.SinaNotifyRequestHandler;
import com.qdlc.p2p.common.core.ReturnResult;
import com.qdlc.p2p.common.core.queue.QueueHandlers;
import com.qdlc.p2p.common.util.NumberUtil;
import com.qdlc.p2p.common.util.OrderNoUtil;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.TppRechargeModel;

/**
 * 功能描述: [新浪][充值]
 * @author linya
 * @date 2016-4-25
 */
@Service
public class AdaptedSinaRechargeService extends AbstractAdaptedRechargeService {

	@Override
	public ReturnResult doRecharge(User user, double money, HttpServletRequest request) {
		//该功能需要
        CreateHostingDepositParam param = new CreateHostingDepositParam();
        param.setOutTradeNo(OrderNoUtil.getInstance().getSerialNumber());
        param.setSummary("在线充值");
        param.setIdentityId(user.getTppUserCustId());
        param.setIdentityIype(IdentityType.UID);
        param.setAccountType(AccountType.SAVING_POT);
        param.setAmount(money);
        DepositExtendParam extendParam = new DepositExtendParam();
        extendParam.setPayType(PayType.online_bank);
        extendParam.setCardType(CardType.DEBIT);
        extendParam.setCardAttribute(CardAttribute.C);

        param.setDepositExtendParam(extendParam);
		return ReturnResult.SUCCESS;
	}

	@Override
	public ReturnResult notifying(HttpServletRequest request, HttpServletResponse response) {

		log.info("充值异步回调 start...");
		
		//获取充值后的通知请求
		SinaNotifyRequestHandler notifyReqeust = SinaNotifyRequestHandler.build(request);
		//构造队列事件参数，根据充值状态来执行成功/失败任务
		String depositStatus = notifyReqeust.getParam("deposit_status");
		String outTradeNo = notifyReqeust.getParam("outer_trade_no");
		String depositAmount = notifyReqeust.getParam("deposit_amount");
		TppRechargeModel rechargeModel = new TppRechargeModel();
		rechargeModel.setOrdNo(outTradeNo);
		rechargeModel.setInMoney(NumberUtil.getDouble2(depositAmount));
		RechargeValueEvent valueEvent = new RechargeValueEvent();
		//将事件放入到队列中
		QueueHandlers.put(valueEvent);
		log.info("充值异步任务已放入到队列中.");
		//写出验签的响应结果
		notifyReqeust.writeResponse(response);
		return ReturnResult.SUCCESS;
	}

	@Override
	public ReturnResult authRecharge(User user, double money, HttpServletRequest request) {
		return null;
	}

	@Override
	public AdaptePlatform supportPlatform() {
		return AdaptePlatform.SINA;
	}

}
